<?php
/**
 * Abstraktny zaklad vsetkych Controllerov
 * 
 * @author Tomas Srna
 * @package bakalarka
 */

abstract class Controller extends Singleton
{
	public $args = Array();
	protected $resource = 'default';
	
	/**
	 * Funkcia automaticky zavolana pred volanim akcie Controlleru
	 */
	public function preDispatch() {
		if($this->args[0]  != 'ActionController' && $this->args[1] != 'login')
			Auth::instance()->requireAuth();
		
		if(!Acl::instance()->allowed($this->resource))
			throw new PermissionDeniedException(
				"", $this->resource);
	}
	
	/**
	 * Funkcia automaticky zavolana po volani akcie Controlleru
	 */
	public function postDispatch() {}
	
	/**
	 * Sikovna funkcia pre automaticky redirect
	 */
	public function autoredir($params = NULL) 
	{
		if($params == NULL)
			$params = $_GET;
		
		$redir = APPBASE;
		
		if(array_key_exists('redir', $_GET))
			$redir = $_GET['redir'];
		if(array_key_exists('hash', $_GET))
			$redir .= '#'.$_GET['hash'];
			
		header('Location: '.$redir);
		exit();
	}
}
